﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace IMark.eMartSer
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "eMartServices" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select eMartServices.svc or eMartServices.svc.cs at the Solution Explorer and start debugging.
    public class eMartServices : IeMartServices
    {
        public List<SAN_PHAM> LaySanPhamTheoLoai(string ID)
        {
            List<SAN_PHAM> lt = new List<SAN_PHAM>();
            
            List<SAN_PHAM> listSanPham = SanPhamModels.LaySanPhamTheoLoai(long.Parse(ID),new List<SAN_PHAM>());
            
            List<LOAI_SAN_PHAM> listLSP = new List<LOAI_SAN_PHAM>();
            foreach (var item in listSanPham)
            {
                List<HINH_SAN_PHAM> listHSP = new List<HINH_SAN_PHAM>();
                SAN_PHAM sp = new SAN_PHAM();
                foreach (var items in item.HINH_SAN_PHAM)
                {
                    HINH_SAN_PHAM objHinhSanPham = new HINH_SAN_PHAM();
                        objHinhSanPham.Hinh = items.Hinh;
                        objHinhSanPham.ID = items.ID;
                        objHinhSanPham.IDSanPham = items.IDSanPham;
                        listHSP.Add(objHinhSanPham);
                        sp.HINH_SAN_PHAM = listHSP;
                }
                sp.DonGia = item.DonGia;
                sp.GiamGia = item.GiamGia;
                sp.ID = item.ID;
                sp.IDLoaiSanPham = item.IDLoaiSanPham;
                sp.MetaDescription = item.MetaDescription;
                sp.MetaKeyword = item.MetaKeyword;
                sp.MetaTitle = item.MetaTitle;
                sp.MoTaChiTiet = item.MoTaChiTiet;
                sp.MoTaTomTat = item.MoTaTomTat;
                sp.NgayBatDau = item.NgayBatDau;
                sp.NgayKetThuc = item.NgayKetThuc;
                sp.NgaySua = item.NgaySua;
                sp.NgayThem = item.NgayThem;
                sp.SoLuong = item.SoLuong;
                sp.TenSanPham = item.TenSanPham;
                sp.ThuongHieu = item.ThuongHieu;
                sp.TrangThai = item.TrangThai;
                
                lt.Add(sp);
            }
           

            return lt;
        }

        public SAN_PHAM LaySanPhamTheoID(string ID)
        {

            SAN_PHAM item = SanPhamModels.LaySanPhamTheoID(long.Parse(ID));

            List<HINH_SAN_PHAM> listHSP = new List<HINH_SAN_PHAM>();
                SAN_PHAM sp = new SAN_PHAM();
                foreach (var items in item.HINH_SAN_PHAM)
                {
                    HINH_SAN_PHAM objHinhSanPham = new HINH_SAN_PHAM();
                    objHinhSanPham.Hinh = items.Hinh;
                    objHinhSanPham.ID = items.ID;
                    objHinhSanPham.IDSanPham = items.IDSanPham;
                    listHSP.Add(objHinhSanPham);
                }
                sp.HINH_SAN_PHAM = listHSP;
                List<DANH_GIA> ltDanhGia = new List<DANH_GIA>();
                foreach (DANH_GIA Sao in item.DANH_GIA)
                {
                    DANH_GIA gd = new DANH_GIA();
                    gd.SoSao = Sao.SoSao;
                    gd.Ten = Sao.Ten;
                    gd.TieuDe = Sao.TieuDe;
                    gd.TrangThai = Sao.TrangThai;
                    gd.NoiDung = Sao.NoiDung;
                    gd.NgayThem = Sao.NgayThem;
                    gd.NgaySua = Sao.NgaySua;
                    gd.IDSanPham = Sao.IDSanPham;
                    gd.ID = Sao.ID;
                    gd.Email = Sao.Email;
                    ltDanhGia.Add(gd);
                }
                sp.DANH_GIA = ltDanhGia;
                sp.DonGia = item.DonGia;
                sp.GiamGia = item.GiamGia;
                sp.ID = item.ID;
                sp.IDLoaiSanPham = item.IDLoaiSanPham;
                sp.MetaDescription = item.MetaDescription;
                sp.MetaKeyword = item.MetaKeyword;
                sp.MetaTitle = item.MetaTitle;
                sp.MoTaChiTiet = item.MoTaChiTiet;
                sp.MoTaTomTat = item.MoTaTomTat;
                sp.NgayBatDau = item.NgayBatDau;
                sp.NgayKetThuc = item.NgayKetThuc;
                sp.NgaySua = item.NgaySua;
                sp.NgayThem = item.NgayThem;
                sp.SoLuong = item.SoLuong;
                sp.TenSanPham = item.TenSanPham;
                sp.ThuongHieu = item.ThuongHieu;
                sp.TrangThai = item.TrangThai;
                return sp;
        }

        public List<LOAI_SAN_PHAM> LayMenuLoaiSanPham()
        {
            List<LOAI_SAN_PHAM> ltSP = new List<LOAI_SAN_PHAM>();
            List<LOAI_SAN_PHAM> ltLoaiSP =  LoaiSanPhamModels.LayLoaiSanPham();
            foreach (LOAI_SAN_PHAM item in ltLoaiSP)
            {
                LOAI_SAN_PHAM lsp = new LOAI_SAN_PHAM();
                lsp.DacBiet = item.DacBiet;
                lsp.ID = item.ID;
                lsp.MaLoaiCha = item.MaLoaiCha;
                lsp.MetaDescription = item.MetaDescription;
                lsp.MetaKeyword = item.MetaKeyword;
                lsp.MetaTitle = item.MetaTitle;
                lsp.MoTa = item.MoTa;
                lsp.NgaySua = item.NgaySua;
                lsp.NgayTao = item.NgayTao;
                lsp.TenLoai = item.TenLoai;
                lsp.TrangThai = item.TrangThai;
                ltSP.Add(lsp);
            }
            return ltSP;
        }

        public List<BAI_VIET> LayDanhSachBaiViet()
        {
            List<BAI_VIET> bv = TinTucModels.LayTatCaBaiViet();
            List<BAI_VIET> ltbv = new List<BAI_VIET>();
            foreach (BAI_VIET item in bv)
	        {
                ltbv.Add(new BAI_VIET()
                {
                    Hinh = item.Hinh,
                    IDLoaiBaiViet = item.IDLoaiBaiViet,
                    NgayTao = item.NgayTao,
                    NguoiTao = item.NguoiTao,
                    NoiDungChiTiet = item.NoiDungChiTiet,
                    NoiDungTomTat = item.NoiDungTomTat,
                    TieuDe = item.TieuDe,
                    TrangThai = item.TrangThai,
                    ID = item.ID
                });
	        }

            return ltbv;
        }

        public string SoSaoTheoIDSP(string ID)
        {
            SAN_PHAM item = SanPhamModels.LaySanPhamTheoID(long.Parse(ID));
            return Utils.GenerateRation(item.DANH_GIA.ToList()); ;
        }

        public List<HOA_DON> LayDanhSachDonHangTheoKhachHang(string ID)
        {
            List<HOA_DON> ltNewHoaDon = new List<HOA_DON>();
            List<HOA_DON> ltHoaDon = HoaDonModels.GetByIDKH(long.Parse(ID));
            foreach (var item in ltHoaDon)
            {
                HOA_DON objHD = new HOA_DON() { 
                    DiaChi = item.DiaChi,
                    DienThoai = item.DienThoai,
                    Email = item.Email,
                    GhiChu = item.GhiChu,
                    HoTen = item.HoTen,
                    MaHD = item.MaHD,
                    MaKH = item.MaKH,
                    NgayDat = item.NgayDat,
                    PhuongThucThanhThoan = item.PhuongThucThanhThoan,
                    ThanhTien = item.ThanhTien,
                    TrangThai = item.TrangThai
                };
                ltNewHoaDon.Add(objHD);
            }
            return ltNewHoaDon;
        }

        public List<CHI_TIET_HD> LayChiTietHDTheoMaHD(string ID)
        {
            List<CHI_TIET_HD> ltNewList = new List<CHI_TIET_HD>();
            List<CHI_TIET_HD> ltCT = ChiTietHDModels.GetByOrderID(long.Parse(ID));
            foreach (var item in ltCT)
            {
                SAN_PHAM objSanPham = SanPhamModels.LaySanPhamTheoID(item.IDSanPham);
                SAN_PHAM objSP = new SAN_PHAM();
                objSP.TenSanPham = objSanPham.TenSanPham;
                ltNewList.Add(new CHI_TIET_HD() { 
                    DonGia = item.DonGia,
                    GiamGia = item.GiamGia,
                    IDSanPham = item.IDSanPham,
                    MaCT = item.MaCT,
                    MaHD = item.MaHD,
                    SoLuong = item.SoLuong,
                    SAN_PHAM = objSP
                });
            }
            return ltNewList;
        }
    }
}
